/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.client.ui;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.DataArrivedEvent;
import com.smartgwt.client.widgets.grid.events.DataArrivedHandler;
import org.gwtportlets.portlet.smartgwt.client.SmartPortlet;
import za.org.foobar.famon.frontend.client.dto.ApplicationGroupDto;
import za.org.foobar.famon.frontend.client.util.SmartGwtUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Carl Crous
 */
public class ApplicationFailureListGrid extends ListGrid {
    private final SmartPortlet portlet;
    private Map<String, FailureListGrid> map = new HashMap<String, FailureListGrid>();
    public static final String COMPONENT_ID = "applicationGroupGrid";

    public ApplicationFailureListGrid(final SmartPortlet portlet) {
        setID(COMPONENT_ID);
        this.portlet = portlet;
        setDataSource(ApplicationGroupDto.getDataSource(portlet));

        setAutoFetchData(true);
        setCanExpandRecords(true);
        setCanExpandMultipleRecords(false);
        setAutoFetchData(true);
        setWidth100();
        setHeight100();

        ListGridField id = new ListGridField("id");
        id.setHidden(true);

        ListGridField application = new ListGridField("application", "Application");
        ListGridField applicationInstance = new ListGridField("applicationInstance", "Instance");
        ListGridField applicationVersion = new ListGridField("applicationVersion", "Version");

        setFields(id, application, applicationInstance, applicationVersion);

        addClickHandler(SmartGwtUtil.getExpandClickHandler(this));
        addDataArrivedHandler(new DataArrivedHandler() {
            public void onDataArrived(DataArrivedEvent event) {
                GWT.log("Data arrived", null);
                RecordList list = ApplicationFailureListGrid.this.getDataAsRecordList();
                if (list.getLength() == 1) {
                    ApplicationFailureListGrid.this.expandRecord((ListGridRecord)list.get(0));
                }
            }
        });
    }

    @Override
    protected Canvas getExpansionComponent(ListGridRecord r) {
        String id = r.getAttributeAsString("id");
        FailureListGrid grid = map.get(id);
        if (grid == null) {
            grid = new FailureListGrid(portlet, id);
            grid.setInitialCriteria(SmartGwtUtil.getCriteria("applicationGroupId", id));
            map.put(id, grid);
        }
        return grid;
    }
}
